const { Send } = require("../utils/response");
const Gallery = require("../models/gallery");
const Collection = require("../models/collection");
const Wallpaper = require("../models/wallpaper");

class CollectionController {
    static async list(req, res, next) {
        try {
            const list = await Collection.find({})
                .select("+name +createdAt +isActive")
                .populate("galleryId", "name");
            return Send(res, {
                message: "获取成功",
                data: list,
            });
        } catch (error) {
            next(error);
        }
    }
    static async add(req, res, next) {
        try {
            const collection = await Collection.create({
                name: req.body.name,
                thumbnail: req.body.thumbnail,
                galleryId: req.body.galleryId,
            });
            if (!collection) {
                return Send(res, {
                    code: -1,
                    message: "图集添加失败",
                });
            }
            Send(res, {
                message: "图集添加成功",
                data: {
                    id: collection._id,
                },
            });
        } catch (error) {
            next(error);
        }
    }
    static async update(req, res, next) {
        try {
            if (req.body && req.body.name) {
                const isExist = await Collection.findOne({
                    name: req.body.name,
                });
                if (isExist) {
                    return Send(res, {
                        code: -1,
                        message: "该图集已存在",
                    });
                }
            }
            const collection = await Collection.findByIdAndUpdate(
                req.params.id,
                req.body
            );
            if (!collection) {
                return Send(res, {
                    code: -1,
                    message: "该图集不存在",
                });
            }
            Send(res, {
                message: "修改成功",
                data: collection._id,
            });
        } catch (error) {
            next(error);
        }
    }
    static async delete(req, res, next) {
        try {
            const { deletedCount } = await Collection.deleteOne({
                _id: req.params.id,
            });
            if (deletedCount === 0) {
                return Send(res, {
                    message: "该图集不存在",
                });
            }
            return Send(res, {
                message: "图集删除成功",
                data: req.params.id,
            });
        } catch (error) {
            next(error);
        }
    }
    static async wallpapers(req, res, next) {
        const collection = await Collection.findOne({
            _id: req.params.id,
        });
        if (!collection) {
            return Send(res, {
                code: -1,
                message: "该图集不存在",
            });
        }
        const collections = await Wallpaper.find({
            collectionId: req.params.id,
        });
        Send(res, {
            message: "获取图集下的壁纸列表成功",
            data: collections,
        });
    }
}

module.exports = CollectionController;
